package claybird.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import claybird.dom.Handicap;
import claybird.dom.Handicap.Categorie;
import claybird.dom.Person;
import claybird.dom.Result;
import claybird.dom.ShootDay;
import claybird.dom.Team;
@Repository
@Transactional
@SuppressWarnings("unchecked")
public class ResultDao extends BaseRepository<Result>{
	public Result findExisting(ShootDay day,Person pers){
		if ( em.createQuery("select r from "+ Result.class.getName()+" r where r.shootDay = :day and r.person = :person")
				.setParameter("day", day).setParameter("person", pers).getResultList().size()>0){
			
			return (Result) em.createQuery("select r from "+ Result.class.getName()+" r where r.shootDay = :day and r.person = :person")
					.setParameter("day", day).setParameter("person", pers).getSingleResult();
		}
		return null;
    }
	public List<Result> findExistingSortedByRanking(ShootDay day){
		return em.createQuery("select r from "+ Result.class.getName()+" r join r.person pers " +
				"where r.shootDay = :day " +
				"order by r.ranking ,r.rankingWithGuest ,pers.name,pers.forName")
					.setParameter("day", day).getResultList();
    }
	public List<Result> findExistingSortedByPoint(ShootDay day){
		return em.createQuery("select r from "+ Result.class.getName()+" r join r.person pers " +
				"where r.shootDay = :day " +
				"order by r.realValue desc,pers.name,pers.forName")
					.setParameter("day", day).getResultList();
    }
	public List<Result> findResultForTeam(Team team) {
		return em.createQuery("select r from "+ Result.class.getName()+" r join r.person pers " +
				"where r.shootDay = :day " +
				"and pers in :personList")
					.setParameter("day", team.getShootDay())
					.setParameter("personList", team.getListPerson()).getResultList();
		
	}
	public List<Result> findResultForTeamWithoutGuest(Team team) {
		return em.createQuery("select r from "+ Result.class.getName()+" r join r.person pers " +
				"where r.shootDay = :day " +
				"and pers in :personList and pers.guest = false")
					.setParameter("day", team.getShootDay())
					.setParameter("personList", team.getListPerson()).getResultList();
		
	}
	public boolean hasDoubleHighScoreByCategorie(ShootDay day, Categorie cat){
		 Float max = (Float) em.createQuery("select Max(r.realValue) from  "+ Result.class.getName()+" r" +
						" where r.shootDay = :day and r.lastCategorieOfHandicap  = :cat and r.realValue>0")
						.setParameter("day", day)
						.setParameter("cat", cat).getSingleResult();
		 List<Long> listRes = em.createQuery("select count(r) from "+ Result.class.getName()+" r " +
				"where r.shootDay = :day and r.lastCategorieOfHandicap = :cat and r.realValue = :valmax" +
						" group by r.lastCategorieOfHandicap")
					.setParameter("day", day)
					.setParameter("valmax", max)
					.setParameter("cat", cat).getResultList();
		
		 for(Long i: listRes){
			 System.out.println("total ="+ i);
			 if (i>1){
				 return true;
			 }
		 }
		return false;
	}
	public boolean hasEnoughResult(Person pers, int year) {
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 

		Date datemin = new Date();
		Date datemax = new Date();
		try {
			 datemin = ft.parse(year+"-01-01");
			 datemax = ft.parse(year+"-12-31");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Long i = (Long) em.createQuery("select count(r) from "+ Result.class.getName()+" r "  +
				"inner join r.shootDay s where r.person = :pers and s.date < :yearMin and s.date > :yearMax")
				.setParameter("pers", pers)
				.setParameter("yearMin", datemin)
				.setParameter("yearMax", datemax).getSingleResult();
		if (i>=4){
			return true;
		}
		return false;
	}
	public List<Result> getListResultForYear(Person pers,int year) {
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 

		Date datemin = new Date();
		Date datemax = new Date();
		try {
			 datemin = ft.parse(year+"-01-01");
			 datemax = ft.parse(year+"-12-31");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return  em.createQuery("select r from "+ Result.class.getName()+" r " +
				"inner join r.shootDay s where r.person = :pers and s.date > :yearMin and s.date < :yearMax").setParameter("pers", pers)
				.setParameter("yearMin", datemin)
				.setParameter("yearMax", datemax).getResultList();
		
	}
}
